package juc.辅助类;

import java.util.concurrent.CyclicBarrier;

/**
 * 循环栅栏
 *
 * @author: 张从龙
 * time: 2022/11/13 13:42
 */
public class CyclicBarrierDemo {
    private static final int NUMBER = 7;

    public static void main(String[] args) {
        //创建循环栅栏
        CyclicBarrier barrier = new CyclicBarrier(NUMBER, () -> System.out.println("已经集齐七龙珠，召唤神龙"));

        //集齐7个才会激活栅栏
        for (int i = 0; i <= 6; i++) {
//        for (int i = 0; i < 6; i++) {
            new Thread(() -> {
                try {
                    System.out.println(Thread.currentThread().getName() + "颗");
                    //都阻塞该位置
                    barrier.await();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }, String.valueOf(i)).start();
        }
        
    }
}